﻿using System;
using System.Collections.Generic;
using System.Linq;
using MvcEngine.Mvc.StarterSite.Controllers;
using System.Web.Mvc;
using MvcEngine.Mvc.Models.ViewModels;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.Blog.Components.Extensions;
using MvcEngine.Blog.Models.ViewData;
using MvcEngine.Core;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Blog.Repositories;
using MvcEngine.Core.Repositories;
using MvcEngine.Core.Helpers;
using MvcEngine.Blog.Helper;
using System.Web;
using MvcEngine.Mvc.Filters;
using MvcEngine.Core.Exceptions;
using MvcEngine.Core.Configuration;
using MvcEngine.Blog.Components.Attributes;
using MvcEngine.Mvc;
using MvcEngine.Mvc.Models;
using System.IO;
using MvcEngine.Core.Extensions;
using MvcEngine.Blog.Managers;
using MvcEngine.Core.Content;
using MvcEngine.Blog.Models;
using MvcEngine.Mvc.Controllers;
using MvcEngine.Core.Pagination;

namespace MvcEngine.Blog.StarterSite.Controllers
{
    public class BlogRecentPostsController : SubController
    {
        public MvcEngineModel Index(int ctrId)
        {
            var blogRecentPostsContent = ContentManager.Current.GetPageContent(ctrId);
            RecentPostsPageContent content = new RecentPostsPageContent(blogRecentPostsContent);

            var blog = BlogManager.Current.GetBlogByPage(blogRecentPostsContent.PageId);
            var posts = BlogPostManager.Current.GetPosts(blog.Id, content.PostCount);

            var result = new List<BlogRecentPostsViewModel>();
            foreach (var item in posts)
            {
                var model = new BlogRecentPostsViewModel()
                {
                    PostId = item.Id,
                    Title = item.Title,
                    PublishedDate = item.PublishedDate.Value
                };
                result.Add(model);
            }

            return new MvcEngineModelList<BlogRecentPostsViewModel>()
            {
                List = result
            };
        }
    }
}
